package com.ymatou.iapi.productmanager.testcase;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.brand.BrandDto;
import com.ymatou.iapi.productmanager.parameter.brand.GetCommonBrandsResponse;
import com.ymatou.iapi.productmanager.service.GetAllCommonBrandsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_GetAllCommonBrands {
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取品牌信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetAllCommonBrandsCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAllCommonBrands_001() {
		Logger.start(true, "获取所有 品牌信息");
		try{
			checkGetAllCommonBrands("");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAllCommonBrands_002() {
		Logger.start(true, "获取晚于某一时间的品牌信息");
		try{
			checkGetAllCommonBrands("2017-11-01");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	public static void checkGetAllCommonBrands(String SelectDate) throws Exception{
		JSONObject ret = new GetAllCommonBrandsCall().call(SelectDate);
		Logger.verifyEquals(200, ret.getInt("Code"),"返回码检查");
		
		GetCommonBrandsResponse actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"), GetCommonBrandsResponse.class, "yyyy-MM-dd HH:mm:ss");
		GetCommonBrandsResponse tgtBean =  getGetCommonBrandsResponseFromSql(SelectDate);
		Logger.verifyEquals(tgtBean.getBrands().size(), actBean.getBrands().size(), "品牌个数应相同");
		AssertHelper.assertResultEqual(tgtBean, actBean, "获取品牌信息检查");
	}
	
	public static GetCommonBrandsResponse getGetCommonBrandsResponseFromSql(String SelectDate){
		List<Map<String,Object>> retFromDB ;
		
		if (SelectDate!=null&& !SelectDate.equals("")){
			retFromDB = intergratedProductMapper.selectAllBrandsAfterSelectDate(SelectDate);
		}else{
			retFromDB = intergratedProductMapper.selectAllBrands();
		}
		
		
		
		
		GetCommonBrandsResponse ret = new GetCommonBrandsResponse();
		List<BrandDto> brandDtoList = new ArrayList<>();
		
		for (Map<String,Object> map:retFromDB){
			BrandDto brandDto = new BrandDto();
			brandDto.setBrandId((Integer)map.get("iBrandId"));
			brandDto.setBrandNameCH(map.get("sBrand")==null?"":map.get("sBrand").toString());
			brandDto.setBrandName(map.get("sBrandEn")==null?"":map.get("sBrandEn").toString());
			brandDto.setAddTime((Date)map.get("dAddTime"));
			brandDto.setUpdateTime(map.get("dUpdateTime")==null?null:(Date)map.get("dUpdateTime"));
			brandDto.setAction((Integer)map.get("iAction"));
			brandDtoList.add(brandDto);
		}
		ret.setBrands(brandDtoList);
		return ret;
	}
	

}
